import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Broadcaster } from '@ionic-native/broadcaster';
import { LoadingController, Loading } from 'ionic-angular';

import {InterfaceListProvider} from '../../providers/interface-list/interface-list';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  isLoading: number = 0;
  pageIndx: number = 0;
  keyword: string = "bilibili";
  items : any = [];
  constructor(public navCtrl: NavController,
              public interface_lists: InterfaceListProvider,
              public loadingCtrl: LoadingController,
              public broadcaster: Broadcaster) {}

  onRefresh(refresher){
    this.pageIndx = 0;
    this.interface_lists.httpGetNewsList(this.keyword,this.pageIndx,20).then((returnData) => {
      this.items = returnData.data.newsList;
      if (refresher == null) return;
      refresher.complete();
    })
  }

  onLoadMore(infiniteScroll){
    this.pageIndx += 1;
    this.interface_lists.httpGetNewsList(this.keyword,this.pageIndx,10).then((returnData) => {

      //var res = JSON.stringify(returnData);
      //console.log(res);

      this.items = this.items.concat(returnData.data.newsList);
      if (infiniteScroll == null) return;
      if (returnData.data.newsList.length == 0){
        this.pageIndx -= 1;
        infiniteScroll.enable(false);
      }
      else {
        infiniteScroll.complete();
      }
    })
  }

  onLoading () {
    let loading = this.loadingCtrl.create({
      content: '加载中请稍后...'//数据加载中显示
    });

    loading.present();
    setTimeout(() => {
      loading.dismiss();//显示多久消失
    }, 5000);
  }

  tabItemClick (model){
    //console.log(model);
    this.navCtrl.push('BrowserPage', {
      browser: {
        title: model.title,
        // url: '这里放置访问的页面链接'
        url: model.originalUrl
      }
    });
  }

  ionViewDidLoad(){
    this.onRefresh(null);
  }


}
